package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.personal.enuminterface.TimeUnitEnums;
import com.zyd.blog.personal.enuminterface.UserType;
import com.zyd.blog.personal.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * UserNo表的业务逻辑层实现类
 * @author mfy
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserNoService userNoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private UserFocusService userFocusService;

    @Resource
    private UserFansService userFansService;

    @Resource
    private UserSimpleInfoService userSimpleInfoService;

    @Resource
    private UserProductionService userProductionService;

    @Resource
    private UserCollectionService userCollectionService;

    @Resource
    private UserThumbService userThumbService;

    @Resource
    private UserShareService userShareService;

    @Resource
    private UserCommentService userCommentService;

    @Resource
    private UserCompanyService userCompanyService;

    @Override
    public Object getUserInfo(Long userId,Long selfId){
        Map<String,Object> map = new HashMap<>(16);
        if(selfId != null){
            if(userFocusService.isHaveFocus(null,selfId,userId,Status.STATUS_YES)){
                map.put("isFocus","Y");
            }else{
                map.put("isFocus","N");
            }
        }else{
            map.put("isFocus","N");
        }
        //  获得用户基本信息
        List<Map<String,Object>> userInfo = userInfoService.getUserInfo(userId);

        if(userInfo.size() > 0){
            System.out.println(userInfo);
            String step = (String) userInfo.get(0).get("step");
            map.put("isApply","Y");
            switch (step){
                case "2":
                    map.put("isSuccess","Z");
                    break;
                case "3":
                    map.put("isSuccess","Y");
                    map.put("isFrist",userInfo.get(0).get("isFrist"));
                    break;
                case "4":
                    map.put("isSuccess","N");
                    break;
                default:
                    map.put("isApply","N");
            }
            map.put("username",userInfo.get(0).get("username"));
            map.put("headPicture",userInfo.get(0).get("headPicture"));
            map.put("nickname",userInfo.get(0).get("nickname"));
            map.put("sex",userInfo.get(0).get("sex"));
            map.put("userId",userInfo.get(0).get("userId"));
            String userType = "userType";
            if(UserType.USER_TYPE_DESIGNER.equals(userInfo.get(0).get(userType))){
                map.put("academic",userInfo.get(0).get("academic"));
                map.put("major",userInfo.get(0).get("major"));
                Long experience = ((new Date()).getTime() - (Long) userInfo.get(0).get("designTime"))/ TimeUnitEnums.YEAR;
                map.put("experience",experience);
                map.put("telephone",userInfo.get(0).get("telephone"));
                map.put("userType","设计师");
                Map<String,Object> userCompanyInfo = userCompanyService.getDesignerInfo(userId);
                if(userCompanyInfo != null){
                    map.put("companyName",userCompanyInfo.get("companyName"));
                    map.put("workAddress",userCompanyInfo.get("workAddress"));
                }
            }else{
                map.put("userType","普通用户");
            }

        }else{
            //  该用户未填写基本信息
            return new HashMap<>(0);
        }

        //  获得用户关注信息
        List<Map<String,Object>> focus = userFocusService.getUserFocusInfo(userId,null,Status.STATUS_YES);
        map.put("focusNum",focus.size());

        //  获取用户粉丝信息
        List<Map<String,Object>> fans = userFansService.getUserFansInfo(userId);
        map.put("fansNum",fans.size());
        return map;
    }

    @Override
    public Object getFocusInfo(Long userId,Long focusId){
        List<Map<String,Object>> result = new ArrayList<>();
        //  获得用户关注信息
        List<Map<String,Object>> focus = userFocusService.getUserFocusInfo(userId,focusId,Status.STATUS_YES);
        for(Map<String,Object> item : focus){
            System.out.println(item);
            Long focusId1 = (Long) item.get("focusId");
            //  获得用户基本信息
            Map<String,Object> map = userSimpleInfoService.getUserSimpleInfo(focusId1);
            if(Status.STATUS_YES.equals(item.get("isFocus"))){
                map.put("isFocus","Y");
            }
            if(Status.STATUS_NO.equals(item.get("isFocus"))){
                map.put("isFocus","N ");
            }
            result.add(map);
        }
        return result;
    }

    @Override
    public Object getFansInfo(Long userId){
        List<Map<String,Object>> result = new ArrayList<>();

        //  获取用户粉丝信息
        List<Map<String,Object>> fans = userFansService.getUserFansInfo(userId);
        for(Map<String,Object> item : fans){
            Long fansId = (Long) item.get("userId");
            Map<String,Object> map = userSimpleInfoService.getUserSimpleInfo(fansId);
            List<Map<String,Object>> focus = userFocusService.getUserFocusInfo(userId,fansId,Status.STATUS_DEFAULT);
            if(focus == null || focus.size() == 0){
                map.put("isFocus","N");
            }else{
                map.put("isFocus","Y");
            }
            result.add(map);
        }
        return result;
    }

    @Override
    public Object focusUser(Long userId,Long focusUserId){
        return userFocusService.focusOtherUser(userId,focusUserId);
    }

    @Override
    public Object noFocus(Long userId,Long focusUserId){
        return userFocusService.noFocus(userId,focusUserId);
    }

    @Override
    public Object collectionProduction(Long userId,Long productionId){
        if(userNoService.getUserNoByCondition(userId, IsDelete.IS_DELETE_NO, Status.STATUS_YES).size() == 0){
            return false;
        }
        if(userProductionService.getUserProduction(null,productionId,null,Status.STATUS_YES,IsDelete.IS_DELETE_NO).size() == 0){
            return false;
        }
        if(userProductionService.getUserProduction(null,productionId,userId,Status.STATUS_YES,IsDelete.IS_DELETE_NO).size() > 0){
            return false;
        }
        List<Map<String,Object>> collections = userCollectionService.getCollectionInfo(userId,productionId,Status.STATUS_DEFAULT);
        if(collections == null || collections.size() == 0){
            return userCollectionService.addCollection(userId,productionId) > 0;
        }else{
            String status = "status";
            if(Status.STATUS_YES.equals(collections.get(0).get(status))){
                return false;
            }else{
                return userCollectionService.updateCollection(userId,productionId,Status.STATUS_YES) > 0;
            }
        }
    }

    @Override
    public Object noCollectionProduction(Long userId,Long productionId){
        if(userNoService.getUserNoByCondition(userId, IsDelete.IS_DELETE_NO, Status.STATUS_YES).size() == 0){
            return false;
        }
        List<Map<String,Object>> collections = userCollectionService.getCollectionInfo(userId,productionId,Status.STATUS_DEFAULT);
        if(collections == null || collections.size() == 0){
            //  并未收藏
            return false;
        }else{
            String status = "status";
            if(Status.STATUS_YES.equals(collections.get(0).get(status))){
                return userCollectionService.updateCollection(userId,productionId,Status.STATUS_NO) > 0;
            }else{
                //  并未收藏
                return false;
            }
        }
    }

    @Override
    public Object getThumbNum(Long productionId){
        Map<String,Object> result = new HashMap<>(16);
        result.put("thumbNum",userThumbService.getThumbInfoByCondition(null,productionId,Status.STATUS_YES).size());
        return result;
    }

    @Override
    public Object thumb(Long userId,Long productionId){
        return userThumbService.thumb(userId,productionId,Status.STATUS_YES);
    }

    @Override
    public Object noThumb(Long userId,Long productionId){
        return userThumbService.thumb(userId,productionId,Status.STATUS_NO);
    }

    @Override
    public Object getShare(Long userId,Long productionId){
        return  userShareService.getShareByUserId(null,userId,productionId,Status.STATUS_YES);
    }

    @Override
    public Object share(Long userId,Long productionId){
        return userShareService.shareProduction(null,userId,productionId,Status.STATUS_YES);
    }

    @Override
    public Object noShare(Long shareId){
        return userShareService.shareProduction(shareId,null,null,Status.STATUS_NO);
    }

    @Override
    public Object getComment(Long productionId){
        return userCommentService.getAllCommentByProductionId(productionId);
    }

    @Override
    public Object comment(Long userId,Long productionId,Long commentId,String content,String ch){
        return userCommentService.addComment(userId,productionId,commentId,content,ch);
    }

    @Override
    public Object deleteCommentPr(Long userId,Long commentPrId){
        return userCommentService.deleteComment(userId,commentPrId,null,"1");
    }

    @Override
    public Object deleteCommentCo(Long userId,Long commentCoId){
        return userCommentService.deleteComment(userId,null,commentCoId,"1");
    }

    @Override
    public Object getDesignerInfo(Long userId){
        return userCompanyService.getDesignerInfo(userId);
    }

    @Override
    public Object getFacousOfFacous(Long userId,Long otherId){
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>> facous = userFocusService.getUserFocusInfo(otherId,null,Status.STATUS_YES);
        for(Map<String,Object> item : facous){
            Long focusId = (Long) item.get("focusId");
            Map<String,Object> userSimple = userInfoService.getUserSimpleInfo(focusId);
            if(userSimple == null || userSimple.size() == 0){
                continue;
            }
            if(userId == null){
                userSimple.put("isFocus","N");
            }else{
                if(userFocusService.isHaveFocus(null,userId,focusId,Status.STATUS_YES)){
                    userSimple.put("isFocus","Y");
                }else{
                    userSimple.put("isFocus","N");
                }
            }
            result.add(userSimple);
        }
        return result;
    }

    @Override
    public Object getFacusOfFan(Long userId,Long otherId){
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>> fan = userFansService.getUserFansInfo(otherId);
        System.out.println(fan);
        for(Map<String,Object> item : fan){
            Long fanId = (Long) item.get("userId");
            Map<String,Object> userSimple = userInfoService.getUserSimpleInfo(fanId);
            if(userId == null){
                userSimple.put("isFocus","N");
            }else{
                if(userFocusService.isHaveFocus(null,userId,fanId,Status.STATUS_YES)){
                    userSimple.put("isFocus","Y");
                }else{
                    userSimple.put("isFocus","N");
                }
            }
            result.add(userSimple);
        }
        return result;
    }
}
